package tt.dz.service.imp.sys;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import tt.dz.dao.base.BaseDao;
import tt.dz.dao.ext.system.InstructionLogDao;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.base.JsonPageForList;
import tt.dz.entity.charger.OmsCharger;
import tt.dz.entity.system.InstructionLog;
import tt.dz.service.imp.base.BaseService;
import tt.dz.service.imp.middleware.instruction.Ev3c2Instructions;
import tt.dz.service.sys.InstructionLogService;
import tt.dz.util.StringUtil;
import tt.dz.util.OmsConstant.MsgCodeEnum;

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class InstructionLogServiceImpl extends BaseService implements InstructionLogService {
	private Log log = LogFactory.getLog(InstructionLogServiceImpl.class);

	@Autowired
	private BaseDao baseDao;

	@Autowired
	private InstructionLogDao inslogDao;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public JsonForList findListByPage(InstructionLog insLog, int pageNo, int pageSize, String sortName, int sortVal) {
		StringBuilder sb0 = new StringBuilder("select * from instruction_log ");
		StringBuilder sb = new StringBuilder(" where  1 = 1 ");
		if (insLog.getTransfer_type() != null) {
			sb.append(" and transfer_type = " + insLog.getTransfer_type());
		}
		
		//模糊查询
		if (StringUtil.isNotBlank(insLog.getCharger_id())) {
			sb.append(" and ( charger_id like '%" + insLog.getCharger_id() + "%'");
		}
		
		if (StringUtil.isNotBlank(insLog.getFrame_type())) {
			sb.append(" or frame_type like '%" + insLog.getFrame_type() + "%'");
		}
		if (StringUtil.isNotBlank(insLog.getIns_content())) {
			sb.append(" or ins_content like '%" + insLog.getIns_content() + "%'");
		}
		if (StringUtil.isNotBlank(insLog.getIns_name())) {
			sb.append(" or ins_name like '%" + insLog.getIns_name() + "%' )");
		}
		List<InstructionLog> list = inslogDao.findListByPageAndGroupAndOrderAndCondition(new InstructionLog(),
				sb.toString(), pageNo, pageSize, sortName, sortVal, "");
		int total = 0;
		StringBuilder countStr = new StringBuilder("select count(1) from ( ");
		try {
			// total = omsChargerDao.queryTotalBySql(new OmsCharger(),
			// countStr.toString()+selectStr.toString()+") b", new
			// ArrayList<Object>());
			total = inslogDao.queryTotalBySql(new InstructionLog(),
					countStr.toString() + sb0.toString() + sb.toString()  + ") b", new ArrayList<Object>());
			inslogDao.exec("delete from instruction_log where datediff(curdate(), create_time)>=2");
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		JsonPageForList jfl = new JsonPageForList();
		jfl.setCode(MsgCodeEnum.SUCCESS.getMsgCode());
		jfl.setList(list);
		jfl.setTotal(total);
		jfl.setMsg(MsgCodeEnum.SUCCESS.getMsgInfo());
		return jfl;
	}

	@Override
	public void add(InstructionLog insLog) throws Exception {
		inslogDao.save(insLog);
	}

	@Override
	public void logicalDel(Integer id) throws Exception {
		InstructionLog insLog = new InstructionLog();
		insLog.setId(id);
		inslogDao.LogicDeleteObject(id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void sendMsg(String msg,int transferType, String chargerId) throws Exception {
		try {
//			log.info("writeLog begin");
			InstructionLog insLog = new InstructionLog();
			if(msg.indexOf("data")>=0){
				msg = msg.substring(9);
			}
			String frameType = msg.substring(6, 8);
			log.info("frameType = "+frameType);
			insLog.setCharger_id(chargerId);
			insLog.setCreate_time(new Date());
			insLog.setTransfer_type(transferType);
			insLog.setIns_content(msg);
			insLog.setValid_flag(Integer.valueOf(1));
			insLog.setFrame_type(frameType);
			String insName = Ev3c2Instructions.parse(frameType).description();
			log.info(insName);
			insLog.setIns_name(insName);
			inslogDao.add(insLog);
//			log.info("writeLog end");
		} catch (Exception e) {
			log.info("writeLog exception:"+e.getMessage());
		}		
	}

	public static void main(String[] args){
		String msg = "data=0134680800A20001501530000000009510000000002017060714040000012017060802174100015015300000000095&ip=182.150.21.104&port=34067&fd=18"; 
//		String msg = "680800A20001501530000000009510000000002017060714040000012017060802174100015015300000000095&ip=182.150.21.104&port=34067&fd=18"; 

//		System.out.println(msg.indexOf("68"));
//		System.out.println(msg.substring(msg.indexOf("68")));
		System.out.println(msg.substring(9));
	}
	
}
